package com.cloud.workmanagerdemo

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.work.Data
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

   fun mainWork(view: View?) {
        val oneTimeWorkRequest: OneTimeWorkRequest =
            OneTimeWorkRequest.Builder(MainWorker::class.java)
                .setInitialDelay(2, TimeUnit.SECONDS)
                .build()
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest)
    }

    fun transferDataWork(view: View?) {
        val data = Data.Builder()
            .putString("data", "Activity的数据").build()
        val oneTimeWorkRequest: OneTimeWorkRequest = OneTimeWorkRequest.Builder(TransferDataWork::class.java)
                .setInputData(data)
                .build()
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest)
        //监听Worker回传的数据
        WorkManager.getInstance(this)
            .getWorkInfoByIdLiveData(oneTimeWorkRequest.id)
            .observe(this) { value ->
                if (value.state.isFinished) {
                    val outputData = value.outputData
                    Log.e("传递数据任务", outputData.getString("data")!!)
                }
            }
    }

    /**
     * 任务链
     * @param view
     */
    fun moreWork(view: View?) {
        val workerRequests: ArrayList<OneTimeWorkRequest> = ArrayList()
        //任务1
        val oneTimeWorkRequest1: OneTimeWorkRequest =
            OneTimeWorkRequest.Builder(DisPatchWork::class.java)
                .setInputData(Data.Builder().putString("data", "任务1").build())
                .build()
        //任务2
        val oneTimeWorkRequest2: OneTimeWorkRequest =
            OneTimeWorkRequest.Builder(DisPatchWork::class.java)
                .setInputData(Data.Builder().putString("data", "任务2").build())
                .build()
        workerRequests.add(oneTimeWorkRequest1)
        workerRequests.add(oneTimeWorkRequest2)

        //任务3
        val oneTimeWorkRequest3: OneTimeWorkRequest =
            OneTimeWorkRequest.Builder(DisPatchWork::class.java)
                .setInputData(Data.Builder().putString("data", "任务3").build())
                .build()
        WorkManager.getInstance(this)
            .beginWith(workerRequests) //先执行的任务
            .then(oneTimeWorkRequest3) //后执行的任务
            .enqueue()
    }

    /**
     * 周期性任务
     * @param view
     */
    fun periodicTask(view: View?) {
        val periodicWorkRequest: PeriodicWorkRequest =
            PeriodicWorkRequest.Builder(PeriodicWork::class.java, 15, TimeUnit.MINUTES)
                .build()
        WorkManager.getInstance(this)
            .enqueue(periodicWorkRequest)
    }
}